package com.example.pet.dao;

import com.hj.pet.model.FriendsRel;
import com.hj.pet.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FriendsRelDao {
    @Insert("insert into friends_rel(id,user_1,user_2,rel_status,detail,create_time,modify_time) " +
            "value(#{id},#{user_1},#{user_2},#{rel_status},#{detail},now(),now())")
    int save (FriendsRel friendsRel);

    @Update("<script>" +
            "update friends_rel" +
            "        <set>" +
            "            <if test=\"null !=rel_status\">" +
            "                rel_status=#{rel_status}," +
            "            </if>" +
            "            <if test=\"null !=detail\">" +
            "                detail=#{detail}," +
            "            </if>" +
            "            <if test=\"null !=modify_time\">" +
            "                modify_time=now()," +
            "            </if>" +
            "        </set>"+
            "     where user_1=#{user_1} and user_2=#{user_2}"
            +"</script>")
    int updateByFriendRel(FriendsRel friendsRel);

    @Select("select * from friends_rel where id=#{id}")
    FriendsRel findRelById(int id);

    @Select("select * from friends_rel where user_1=#{user_1}")
    List<FriendsRel> findRelsByUser1(String user_1);

    @Select("select * from friends_rel where user_2=#{user_2}")
    List<FriendsRel> findRelsByUser2(String user_2);

    @Delete("delete from friends_rel where user_1=#{user_1},user_2=#{user_2}")
    int delRelByUser(String user_1, String user_2);

    @Select("<script>" +
            "select * from friends_rel" +
            "        <where>" +
            "            <if test=\"null !=user_1\">" +
            "                and user_1=#{user_1}" +
            "            </if>" +
            "            <if test=\"null !=user_2\">" +
            "                and user_2=#{user_2}" +
            "            </if>" +
            "            <if test=\"null !=rel_status\">" +
            "                and rel_status=#{rel_status}" +
            "            </if>" +
            "        </where>"
            +"</script>")
    List<FriendsRel> findRels(FriendsRel friendsRel);

    // TODO 自己测
    @Select("select a.user_account,a.user_name from user_info a ,friends_rel b" +
            " where a.user_account=b.user_1 and rel_status = 2")
    List<UserInfo> getFriends(String user_1);

    @Select("select count(*) from user_info where user_account = #{account}")
    int searchByAccount(String account);

    @Select("select * from friends_rel where user_1 =#{user1} and rel_status=#{status}")
    List<FriendsRel> searchRelByUser1AndStatus(String user1, Integer status);

    @Select("select * from friends_rel where user_2 =#{user2} and rel_status=#{status}")
    List<FriendsRel> searchRelByUser2AndStatus(String user2, Integer status);

    @Select("select count(*) from friends_rel where user_1=#{user1} and user_2=#{user2} and rel_status=#{status}")
    int searchByUser1AndUser2AndStatus(String user1, String user2, Integer status);

    // TODO 返回记录的个数

}
